<?php defined('BASEPATH') OR exit('No direct script access allowed');
use EasyWeChat\Foundation\Application;
class Fans extends Admin_Controller
{
    private $avatar_path;
    function __construct ()
    {
        parent::__construct();
        $this->config->load('userset');
        $this->load->library('pagination');
        $this->uid = $this->session->userdata('user_id');
        $this->avatar_path = FCPATH.'uploads/';

    }

    public function index($page=1)
    {
        $limit = 30;
        $config['uri_segment'] = 4;
        $config['use_page_numbers'] = TRUE;
        $config['base_url'] = site_url('manage/fans/index/');
        if(!$this->auth->is_super_admin()){
            $config['total_rows'] = $this->user_m->count_user_list_by(4,$this->uid);
        }else{
            $config['total_rows'] = $this->user_m->count_user_list_by(4);
        }
        $config['per_page'] = $limit;
        $config['num_links'] = 10;
        $this->pagination->initialize($config);
        $start = ($page-1)*$limit;
        $data['pagination'] = $this->pagination->create_links();
        if(!$this->auth->is_super_admin()){
            $list = $this->user_m->get_user_list_by(4,$start,$limit,$this->uid);
        }else{
            $list = $this->user_m->get_user_list_by(4,$start,$limit);
        }
        if(!empty($list)){
            foreach($list as $key => $value){
                if($value['sex'] == 1){
                    $list[$key]["sex"] = "男";
                }else{
                    $list[$key]["sex"] = "女";
                }
                if(empty($value['user_tel'])){
                    $list[$key]["user_tel"] = "---";
                }
                if(empty($value['agent_id'])){
                    $list[$key]["agent_id"] = "公众号";
                }else{
                    $user_arr = $this->user_m->get_user_by_user_id($value['agent_id']);
                    $list[$key]["agent_id"] = $user_arr['user_name'];
                }
                if(empty($value['user_dress'])){
                    $list[$key]["user_dress"] = "---";
                }

            }
        }
        $data['list'] = $list;
        $this->load->view('fans/index', $data);

    }

    /**
     * 设置部门或者代理商
     */
    public function set_auth_user(){
        if($_POST){
            $post =$this->input->post();
            $user_id = $post['user_id'];
            $user_group = $post['user_group'];
            $user_name = $post['user_name'];
            $user_password = $post['user_password'];
            if(!$user_id || !$user_group){
                echo json_encode(['code' => 400, 'msg' => '参数有误']);exit();
            }
            if($user_group == 3){
                // 判断是否是超级管理员
                if(!$this->auth->is_super_admin()){
                    echo json_encode(['code' => 400, 'msg' =>"没有权限操作"]);exit();
                }
                $url = site_url(X_URL."bumen/index");
            }
            if($user_group == 5){
                $user_arr = $this->user_m->get_user_by_user_id($this->uid);
                if($user_arr['user_group'] >= 4){
                    echo json_encode(['code' => 400, 'msg' =>"没有权限操作"]);exit();
                }
                $url = site_url(X_URL."agent/index");
            }
            //删除该用户的group_id
            $result = $this->groupAcess_m->del_group_id_by_user_id($user_id);
            if($result){
                //插入新的groupid
                $this->groupAcess_m->insert_user_access(array('uid'=>$user_id,'group_id'=>$user_group));
                //更新user表里边的groupid
                $salt =get_salt();
                $user_arr = array(
                    'user_group'=>$user_group,
                    'user_name'=>$user_name,
                    'user_password' =>  password_dohash($user_password,$salt),
                    'user_salt' => $salt,
                );
                $this->user_m->update_user($user_id,$user_arr);
                //生成专属分享海报
                $this->get_qrcode($user_id);
                echo json_encode(['code' => 200, 'msg' => '操作成功','url'=>$url]);exit();
            }else{
                echo json_encode(['code' => 400, 'msg' => '操作失败，请稍后再试']);exit();
            }
        }
    }

    /**
     * 创建带参二维码
     * @param $user_id
     * @return bool|string
     */
    public function get_qrcode($user_id){

        $uid = $user_id;
        $qrcode_pic_url = $this->avatar_path.$uid.'/qrcode/'.$uid.'_qrcode.png'; //微信返回的二维码 430*430

        if(file_exists($qrcode_pic_url)){
            // 已经生成过则不再生成
            $qrcode_url = base_url('uploads/'.$uid.'/qrcode/'.$uid.'_qrcode.png');
            return $qrcode_url;exit();
        }
        $options = [
            'app_id' => config_item("app_id"),
            'secret' => config_item("app_secret"),
        ];
        $app = new Application($options);
        $qrcode = $app->qrcode;
//        $result = $qrcode->forever($uid); // 永久带参二维码
        $result = $qrcode->temporary($uid, 7 * 24 * 3600); // 临时带参二维码
        $url = $qrcode->url($result->ticket);
        $path = $this->avatar_path.$uid.'/qrcode/';
        if(!file_exists($path)){
            mkdir($path,0777,true);
        }
        $content = get_url_content($url); // 得到二进制图片内容
        file_put_contents($qrcode_pic_url, $content); // 写入文件
        if(file_exists($qrcode_pic_url)){
            return true;
        }else{
            return false;
        }
    }

}
